package com.zhang.chatcenter.service.chat.impl;

import com.baomidou.mybatisplus.extension.conditions.query.LambdaQueryChainWrapper;
import com.baomidou.mybatisplus.extension.conditions.update.LambdaUpdateChainWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zhang.chatcenter.chat.ChatMessage;
import com.zhang.chatcenter.common.model.dao.RoomDO;
import com.zhang.chatcenter.common.model.dao.RoomMessageDO;
import com.zhang.chatcenter.common.model.dao.RoomUserDO;
import com.zhang.chatcenter.common.model.enums.RoomTypeEnums;
import com.zhang.chatcenter.dao.RoomMapper;
import com.zhang.chatcenter.dao.RoomMessageMapper;
import com.zhang.chatcenter.dao.RoomUserMapper;
import com.zhang.chatcenter.service.chat.IRoomService;
import com.zhang.infrastructure.model.KingHoodExceptionUtil;
import com.zhang.infrastructure.util.UserUtil;
import com.zhang.infrastructure.util.base.IdGenUtil;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.Objects;

/**
 * description
 *
 * @author zhangFanJun
 * @date 2024-03-17 10:35
 **/
@RequiredArgsConstructor
@Slf4j
@Service
public class RoomService extends ServiceImpl<RoomMapper, RoomDO> implements IRoomService {

    private final RoomMapper roomMapper;
    private final RoomMessageMapper roomMessageMapper;
    private final RoomUserMapper roomUserMapper;

    @Override
    public List<RoomDO> getMyChat(Boolean groupChat) {

        return roomMapper.getMyChat(UserUtil.getUserId(),groupChat);
    }

    @Override
    public List<RoomMessageDO> getRoomContent(Long roomId) {

        return new LambdaQueryChainWrapper<>(roomMessageMapper)
                .eq(RoomMessageDO::getRoomId,roomId)
                .orderByAsc(RoomMessageDO::getCreateTime)
                .list();
    }

    @Override
    public Long newRoom(ChatMessage chatMessage, RoomTypeEnums type) {

        KingHoodExceptionUtil.isEmpty(chatMessage.getToList(),"用户未选择");
        chatMessage.getToList().add(chatMessage.getUserId());
       // 单人聊天的时候，需要查找是否已经存在
        if(Objects.equals(type,RoomTypeEnums.SINGLE)){
            KingHoodExceptionUtil.trueThrows(chatMessage.getToList().size() !=2,"用户不存在");
            RoomDO one = roomMapper.getRoom(chatMessage.getToList().get(0),chatMessage.getToList().get(1));
            if(Objects.nonNull(one)){
                return one.getId();
            }
        }

        RoomDO room = new RoomDO();
        room.setId(IdGenUtil.getId());
        room.setType(type.getType());
        room.setUserId(chatMessage.getUserId());
        room.setName(chatMessage.getMessage());
        room.setCreateTime(new Date());
        room.setUpdateTime(new Date());
        roomMapper.insert(room);

        for (Long userId : chatMessage.getToList()) {
            RoomUserDO roomUserDO = new RoomUserDO();
            roomUserDO.setId(IdGenUtil.getId());
            roomUserDO.setRoomId(room.getId());
            roomUserDO.setUserId(userId);
            roomUserDO.setCreateTime(new Date());
            roomUserMapper.insert(roomUserDO);
        }

        return room.getId();
    }

    @Override
    public void addMessage(Long roomId, Long userId, String message) {

        KingHoodExceptionUtil.isNull(roomId,"房间id不存在");
        RoomMessageDO roomMessageDO = new RoomMessageDO();
        roomMessageDO.setId(IdGenUtil.getId());
        roomMessageDO.setRoomId(roomId);
        roomMessageDO.setUserId(userId);
        roomMessageDO.setMessage(message);
        roomMessageDO.setCreateTime(new Date());
        roomMessageMapper.insert(roomMessageDO);

        new LambdaUpdateChainWrapper<>(roomMapper)
                .eq(RoomDO::getId,roomId)
                .set(RoomDO::getUpdateTime,new Date())
                .update();
    }
}
